package com.leji.song.extra;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.util.SharePreStore;
import com.dgl.sdk.view.WidgetAnim;
import com.google.gson.Gson;
import com.leji.song.R;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.util.ConstAns;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

/**
 * Created by colin.ding on 2014/6/4.
 */
public class LoginActivity extends BaseActivity{

    private EditText phoneNum, checkCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        initView();

    }

    private void initView() {
        setActivityTitle("登录");
        phoneNum = (EditText) findViewById(R.id.phoneNum);
        checkCode = (EditText) findViewById(R.id.checkCode);

        if(MyApplication.isDebug){
            phoneNum.setText("18680484938");
            checkCode.setText("1234");
        }
    }


    public void regeist(){
        if(WidgetAnim.checkIfEmpty(this,  phoneNum)){
           return;
        }

        if(WidgetAnim.checkIfEmpty(this,  checkCode)){
           return;
        }

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_Regist.params.phone, phoneNum.getText().toString());
        params.put(HttpInterface.HIF_Regist.params.captcha, checkCode.getText().toString());

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_Regist.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity) this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

//                String resultStr = (String)NomalResult.doResult(o, LoginActivity.this);
//                if(resultStr!=null){
//                    SharePreStore.save(ConstAns.MY_ID, resultStr);
//                    showActivity(MainTabActivity.class);
//                }

                NomalResult.Result result = NomalResult.getResult(o, LoginActivity.this);

                if(result.Code.equals("0")){
                    SharePreStore.save(ConstAns.MY_ID, result.getReturn().toString());
                    SharePreStore.save(ConstAns.MY_PHONE, phoneNum.getText().toString());

//                    showActivity(MainTabActivity.class);
                    MyApplication.isLogin = true;
                    finish();
                    showToast("已成功注册！");
                }else if(result.Code.equals("2")){

                }
            }
        });


    }

    public  void loginIn(View v) {

        if(WidgetAnim.checkIfEmpty(this,  phoneNum)){
            return;
        }

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_LoginIn.params.phone, phoneNum.getText().toString());

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_LoginIn.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o, LoginActivity.this);
                if (obj != null) {

                    try{

                        NomalResult.Result result = NomalResult.getResult(o, LoginActivity.this);

                        if(result.Code.equals("0")){
                            Gson gson = new Gson();

                            DS_User user = JsonUtil.fromJson(gson.toJson(obj), DS_User.class);

                            SharePreStore.save(ConstAns.MY_ID, user.getId());
                            SharePreStore.save(ConstAns.MY_PHONE, phoneNum.getText().toString());
//                            showActivity(MainTabActivity.class);
                            MyApplication.isLogin = true;
                            finish();
                        }else if(result.Code.equals("2")){
//                             showToast("请先注册！");
                            regeist();
                        }else if(result.Code.equals("1")){

                            showToast("未注册，正在注册！");
                            regeist();
                        }


                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }

            }
        });
    }

    public void getCheckCode(View v){

        if(WidgetAnim.checkIfEmpty(this,  phoneNum)){
           return;
        }


        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetCheckCode.params.phone, phoneNum.getText().toString());

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetCheckCode.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity) this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                String code  = (String)NomalResult.doResult(o, LoginActivity.this);
                if((code!=null) && (!code.equals(""))){
                    showToast("已发短信验证码：" +  code);
                }else{
                    showToast("未获取到短信验证码");
                }

            }
        });

    }



}
